package hao.bk.parking;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import hao.bk.royalmap.GeneralAnimationUtils;
import hao.bk.royalmap.R;
import hao.bk.royalmap.RoyalMapActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ParkingMapLayout extends LinearLayout implements OnClickListener{
	
	private static final int CAMERA_REQUEST = 1888;
	private RoyalMapActivity main;
	private Button btnTakePhoto,previewImage,nextImage;
	public TouchImageView anhChup1, anhChup2, anhChup3;
	public int ANH_1_DISPLAY = 1;
	public int ANH_2_DISPLAY = 2;
	public int ANH_3_DISPLAY = 3;
	public int statusDisplay = ANH_1_DISPLAY;
	public TextView timeTakePhoto;
	public String getTimePhoto1, getTimePhoto2, getTimePhoto3;
	private ParkingGeneralLayout generalLayout;
	private Intent intent;
	public ParkingMapLayout(Context context) {
		super(context);
		initParams();
	}
	/**
	 * Khởi tạo các tham số
	 */
	private void initParams() {
		
		main = RoyalMapActivity.getInstance();
		this.generalLayout = main.parking.generalLayout;
		inflate(main, R.layout.parking_take_photo_layout, this);
		anhChup1 = (TouchImageView)findViewById(R.id.anh_chup_1);
		anhChup2 = (TouchImageView)findViewById(R.id.anh_chup_2);
		anhChup3 = (TouchImageView)findViewById(R.id.anh_chup_3);
		btnTakePhoto = (Button)findViewById(R.id.taking_camera_btn);
		btnTakePhoto.setOnClickListener(this);
		previewImage = (Button)findViewById(R.id.preview_image_btn);
		previewImage.setOnClickListener(this);
		nextImage = (Button)findViewById(R.id.next_image_btn);
		nextImage.setOnClickListener(this);
		timeTakePhoto = (TextView)findViewById(R.id.time_take_photo);
		
	    //Load về các ảnh đã lưu
	    getImageFromSdcard();
	    //Lấy thông tin về thời gian lưu ảnh 1
        getTimeTakePhoto();
	    /**mặc định ảnh chụp đầu tiên được hiển thị trước  */
	    anhChup1.setVisibility(View.VISIBLE);
        statusDisplay = ANH_1_DISPLAY;
        timeTakePhoto.setText(getTimePhoto1);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.taking_camera_btn:
			if(statusDisplay == ANH_1_DISPLAY) {
				//để save ảnh vào sdcard
				main.takePhoto = ANH_1_DISPLAY; //=1
				captureImage();
				break;
			}else if(statusDisplay == ANH_2_DISPLAY){
				main.takePhoto = ANH_2_DISPLAY; //=2
				captureImage();
				break;
				}else if(statusDisplay == ANH_3_DISPLAY){
					main.takePhoto = ANH_3_DISPLAY; //=3
					captureImage();
					break;
				}
		case R.id.preview_image_btn:
			timeTakePhoto.setText("");
			getTimeTakePhoto();
			switch (statusDisplay) {
			case 1://ANH_1_DISPLAY
				previewImage.setVisibility(View.GONE);
				break;
			case 2://ANH_2_DISPLAY
				statusDisplay = ANH_1_DISPLAY;
				timeTakePhoto.setText(getTimePhoto1);
				anhChup2.setVisibility(View.GONE);
				anhChup1.setVisibility(View.VISIBLE);
				GeneralAnimationUtils.startLeftToRight(anhChup2, anhChup1);
				previewImage.setVisibility(View.GONE);
				nextImage.setVisibility(View.VISIBLE);
				
				break;
			case 3://ANH_3_DISPLAY
				statusDisplay = ANH_2_DISPLAY;
				timeTakePhoto.setText(getTimePhoto2);
				anhChup3.setVisibility(View.GONE);
				anhChup2.setVisibility(View.VISIBLE);
				GeneralAnimationUtils.startLeftToRight(anhChup3, anhChup2);
				nextImage.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
			break;
			
		case R.id.next_image_btn:
			timeTakePhoto.setText("");
			getTimeTakePhoto();
			switch (statusDisplay) {
			case 1://ANH_1_DISPLAY
				anhChup1.setVisibility(View.GONE);
				anhChup2.setVisibility(View.VISIBLE);
				GeneralAnimationUtils.startRightToLeft(anhChup1, anhChup2);
				statusDisplay = 2;//ANH_2_DISPLAY;
				timeTakePhoto.setText(getTimePhoto2);
				previewImage.setVisibility(View.VISIBLE);
				break;
			case 2://ANH_2_DISPLAY
				anhChup2.setVisibility(View.GONE);
				anhChup3.setVisibility(View.VISIBLE);
				statusDisplay = 3; //ANH_3_DISPLAY;
				timeTakePhoto.setText(getTimePhoto3);
				GeneralAnimationUtils.startRightToLeft(anhChup2, anhChup3);
				nextImage.setVisibility(View.GONE);
				break;
			case 3://ANH_3_DISPLAY
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
	//Lấy thông tin về thời gian lưu các ảnh, hiển thị lên TextView timeTakePhoto
	public void getTimeTakePhoto() {
		getTimePhoto1 = "";
		getTimePhoto2 = "";
		getTimePhoto3 = "";
		// Lấy thông tin đã lưu hiện lên textView
		String sdcard1 = Environment
				 .getExternalStorageDirectory()
				 .getAbsolutePath()+"/timePhoto1.txt";
		String sdcard2 = Environment
				 .getExternalStorageDirectory()
				 .getAbsolutePath()+"/timePhoto2.txt";
		String sdcard3 = Environment
				 .getExternalStorageDirectory()
				 .getAbsolutePath()+"/timePhoto3.txt";
		
		 try {
		 Scanner scan=new Scanner(new File(sdcard1));
		 String line="";
		 while(scan.hasNext())
		 {
			 line = scan.nextLine();
				getTimePhoto1 += line;
				break;
		 }
		 scan.close();
		
		 } catch (FileNotFoundException e) {
		 e.printStackTrace();
		 }
		 
		 try {
			 Scanner scan=new Scanner(new File(sdcard2));
			 String line="";
			 while(scan.hasNext())
			 {
				 line = scan.nextLine();
					getTimePhoto2 += line;
					break;
			 }
			 scan.close();
			
			 } catch (FileNotFoundException e) {
			 e.printStackTrace();
			 }
		 
		 try {
			 Scanner scan=new Scanner(new File(sdcard3));
			 String line="";
			 while(scan.hasNext())
			 {
				 line = scan.nextLine();
					getTimePhoto3 += line;
					break;
			 }
			 scan.close();
			
			 } catch (FileNotFoundException e) {
			 e.printStackTrace();
			 }
	}
	//Lấy ảnh từ sdcard
	public void getImageFromSdcard() {
		File sdCardDirectory = Environment.getExternalStorageDirectory();
		 File image1 = new File(sdCardDirectory, "anhchup1.png");
		 File image2 = new File(sdCardDirectory, "anhchup2.png");
		 File image3 = new File(sdCardDirectory, "anhchup3.png");
		       // Encode the file as a PNG image.
		       try {

		          FileInputStream inputStream = new FileInputStream(image1);
		           Bitmap b = BitmapFactory.decodeStream(inputStream);
		           if(b != null) {
			           anhChup1.setImageBitmap(b);
		           }
		           
		       } catch (FileNotFoundException e) {
		           e.printStackTrace();
		       } catch (IOException e) {
		           e.printStackTrace();
		       }
		       // Encode the file as a PNG image.
		       try {

		          FileInputStream inputStream = new FileInputStream(image2);
		           Bitmap b = BitmapFactory.decodeStream(inputStream);
		           if(b != null) {
			           anhChup2.setImageBitmap(b);
		           }
		           
		       } catch (FileNotFoundException e) {
		           e.printStackTrace();
		       } catch (IOException e) {
		           e.printStackTrace();
		       }
		       // Encode the file as a PNG image.
		       try {

		          FileInputStream inputStream = new FileInputStream(image3);
		           Bitmap b = BitmapFactory.decodeStream(inputStream);
		           if(b != null) {
			           anhChup3.setImageBitmap(b);
		           }
		           
		       } catch (FileNotFoundException e) {
		           e.printStackTrace();
		       } catch (IOException e) {
		           e.printStackTrace();
		       }
		       
	}
	private void captureImage() {
		Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		main.startActivityForResult(cameraIntent, CAMERA_REQUEST);
		
	}
	
	
}
